import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";

export const server = new McpServer({
  name: "Fulfillment MCP Server",
  version: "1.0.0",
});

const prefix = "https://www.zyysjk.xyz/api/v1";

server.tool("getOrders", "Get product orders", async () => {
  console.error("Fetching orders");
  const res = await fetch("http://localhost:8080/orders");
  const orders = await res.json();

  return { content: [{ type: "text", text: JSON.stringify(orders) }] };
});

// jsdoc
/** @param {import('zod').ZodObject<{ drugName: z.ZodString }>} params */
server.tool(
  "getZhongyooInfo",
  "Get Zhongyoo Database",
  {
    drugName: z.string().describe("The name of the drug to search for."),
  },
  async ({ drugName }) => {
    const res = await fetch(`${prefix}/medinfo/page/name/${drugName}`);
    const data = await res.json();
    // https://<<prefix>>/medinfo/page/:id
    const info = await fetch(`${prefix}/medinfo/page/${data.id}`);
    const infoData = await info.json();

    return {
      content: [{ type: "text", text: JSON.stringify(infoData) }],
    };
  }
);
